package com.feeyo.groundservice.composewidget.view.button

import androidx.compose.animation.animateColorAsState
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.gestures.detectTapGestures
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.BoxScope
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableLongState
import androidx.compose.runtime.mutableLongStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import com.feeyo.groundservice.composewidget.R
import com.feeyo.groundservice.composewidget.util.toComposeColor

/**
 *作者：QLH on 2023-08-17
 *描述：
 */
/**
 * 点击变化背景色的按钮
 * @param lastClickTime 上次点击时间
 * 注意：如果是组按钮则需要提到外面，单按钮则默认即可
 * **/

@Composable
fun CustomButton(
    lastClickTime: MutableLongState = remember { mutableLongStateOf(0) },
    modifier: Modifier = Modifier,
    bgs: List<Color> = listOf(
        R.color.blue_2E76EE.toComposeColor(),
        R.color.blue_2E76EE.toComposeColor().copy(alpha = 0.8f)
    ),
    cornerShape: RoundedCornerShape = RoundedCornerShape(0.dp),
    borderColors: List<Color>? = null,
    borderWidth: Dp = 1.dp,
    onClick: () -> Unit,
    onClickState: (state: Boolean) -> Unit = {},
    intervalClickTime: Int = 500,
    content: @Composable BoxScope.() -> Unit
) {
    val isClick = remember { mutableStateOf(false) }
    val bgColor = animateColorAsState(
        targetValue = if (isClick.value) bgs[1] else bgs[0],
        label = "bgColor"
    )

    Box(
        modifier = Modifier.background(
            color = bgColor.value,
            shape = cornerShape
        ).let {
            if (borderColors != null) {
                val borderColor = animateColorAsState(
                    targetValue = if (isClick.value) borderColors[1] else borderColors[0]
                )
                it.border(
                    width = borderWidth,
                    color = borderColor.value,
                    shape = cornerShape
                )
            } else
                it
        }.then(modifier)
            .pointerInput(lastClickTime.longValue) {
                detectTapGestures(
                    onPress = {
                        isClick.value = true
                        onClickState(true)
                        //awaitRelease()
                        val isUp = tryAwaitRelease()//只监听点击抬起完成，否则阻塞在这里(区别移开操作)
                        isClick.value = false
                        onClickState(false)
                        if (isUp) {
                            //防止重复点击
                            val currentTimeMillis = System.currentTimeMillis()
                            if (currentTimeMillis - intervalClickTime >= lastClickTime.longValue) {
                                onClick()
                                lastClickTime.longValue = currentTimeMillis
                            }
                        }
                    }
                )
            },
    ) {
        content()
    }
}